package DataIO;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

public class ReelleHydraulique {
	
	/** Tableau des apports */
	double apports[];
	
	/** Tableau des vmax */
	double vmax[];
	
	/** v0 */
	double v0;
	
	/** tableau des vmin */
	double vmin[];
	
	/** Rho */
	double rho;
	
	/**
	 * Contruit une instance de reellehydraulique
	 * @param file fichier xls de recours
	 */
	public ReelleHydraulique(String file){
		apports=new double[7];
		vmax=new double[7];
		vmin=new double[7];
		try {
			readData( file);
		} catch (InvalidFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * Lis les datas depuis un fichier xls
	 * @param file Fichier xls recours
	 * @throws InvalidFormatException
	 * @throws IOException
	 */
	public void readData(String file) throws InvalidFormatException, IOException{
		InputStream inp = new FileInputStream(file);
		Workbook wb = WorkbookFactory.create(inp);
		Sheet sheet1 = wb.getSheetAt(6);
		ArrayList<Row> rows = new ArrayList<Row>();
		int rowindexs[]={1,3,7};
		for(int i : rowindexs){
			rows.add(sheet1.getRow(i));
		}

		int columsIndex[] = {2,4,6,8,10,12,14};
		int j=0;//Colonne
		for (int ci : columsIndex){

			ArrayList<Cell> cells = new ArrayList<Cell>();
			for(Row row :  rows){
				cells.add(row.getCell(ci));
			}
			int i = 0 ;//Ligne
			for(Cell cell : cells){

				switch (cell.getCellType()) {

				case Cell.CELL_TYPE_NUMERIC:
					if (!DateUtil.isCellDateFormatted(cell)){
						
							//Cout
							//cout[i]=cell.getNumericCellValue();
						
						switch (i){
						case 0:
							//System.out.println("Apports: "+cell.getNumericCellValue());
							apports[j]=cell.getNumericCellValue();
							break;
						case 1:
							//System.out.println("Vmax : "+cell.getNumericCellValue());
							vmax[j]=cell.getNumericCellValue();
							break;
						case 2:
							//System.out.println("Vmin : "+cell.getNumericCellValue());
							vmin[j]=cell.getNumericCellValue();
							break;
						default:
								
						}
					}
					break;
				default:
					System.out.println("Probl�me de format de cellule");
				}
				i++;
			}
			j++;
		}
		
		v0=sheet1.getRow(5).getCell(1).getNumericCellValue();
		rho=Double.valueOf(sheet1.getRow(9).getCell(0).getRichStringCellValue().getString().substring(4));
		inp.close();
	
	}

	
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "ReelleHydraulique [apports=" + Arrays.toString(apports)
				+ ", vmax=" + Arrays.toString(vmax) + ", v0=" + v0 + ", vmin="
				+ Arrays.toString(vmin) + ", rho=" + rho + "]";
	}
	
	
	/**
	 * @return the apports
	 */
	public double[] getApports() {
		return apports;
	}
	/**
	 * @return the vmax
	 */
	public double[] getVmax() {
		return vmax;
	}
	/**
	 * @return the v0
	 */
	public double getV0() {
		return v0;
	}
	/**
	 * @return the vmin
	 */
	public double[] getVmin() {
		return vmin;
	}
	/**
	 * @return the rho
	 */
	public double getRho() {
		return rho;
	}
	
	
}
